package com.hqyj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.ManagerManagerInfoDao;
import com.hqyj.pojo.ManagerManagerInfo;
import com.hqyj.util.MdFive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
public class ManagerManagerInfoServiceImpl implements ManagerManagerInfoService {
    @Autowired(required = false)
    ManagerManagerInfoDao managerManagerInfoDao;
    @Autowired
    MdFive mdfive;

    @Override
    public HashMap<String, Object> managerselect(ManagerManagerInfo manager) {
        HashMap<String,Object> map=new HashMap<String, Object>();
        //根据用户选择的查询条件
        PageHelper.startPage(manager.getPage(),manager.getRow());
        List<ManagerManagerInfo> list= new ArrayList<>();
        if(manager.getCondition().equals("经理编号")){
            //设置用户输入的查询条件
            manager.setManagerId(Integer.parseInt(manager.getConValue()));
             list = managerManagerInfoDao.findByManagerId(manager);
        }else if(manager.getCondition().equals("姓名")){
            manager.setManagerName(manager.getConValue());
             list = managerManagerInfoDao.findByManagerName(manager);
        }else {
             list = managerManagerInfoDao.select();
        }



        PageInfo<ManagerManagerInfo> page=new PageInfo<ManagerManagerInfo>(list);
        map.put("list",page.getList());
        map.put("total",page.getTotal());
        map.put("totalPage",page.getPages());
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else {
            map.put("pre",page.getPrePage());
        }

        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        map.put("cur",page.getPageNum());

        return map;
    }

    @Override
    public ManagerManagerInfo selectByManagerId(ManagerManagerInfo manager) {
        return managerManagerInfoDao.selectByManagerId(manager);
    }

    @Override
    public String update(ManagerManagerInfo manager) {
//        int v= managerInfoDao.valName(manager);
//        if (v==0){

        ManagerManagerInfo uSalt = managerManagerInfoDao.selectByManagerId(manager);
        String p = mdfive.encrypt(manager.getManagerPwd(), uSalt.getSalt());
        manager.setManagerPwd(p);
        int num = managerManagerInfoDao.update(manager);
        if (num > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @Override
    public String del(ManagerManagerInfo manager) {
        int num = managerManagerInfoDao.del(manager);
        if(num>0){
            return "删除成功";
        }


        return "删除失败";
    }

    @Override
    public String xinjian(ManagerManagerInfo manager) {
        //查询数据库经理名是否重名
        int num = managerManagerInfoDao.valName(manager);

        if(num>0){
            return "经理名已经被建立";
        }else{
            //新建操作
            //自动生成一个盐值
            Random rd = new Random();
            String salt = rd.nextInt(10000) + "";

            //加密用户输入的密码
            String pwd = mdfive.encrypt(manager.getManagerPwd(), salt);
            //把加过密码的传到数据层中
            manager.setManagerPwd(pwd);
            //存入盐值
            manager.setSalt(salt);
            //注册
            int n = managerManagerInfoDao.xinjian(manager);
            if(n>0){
                return"新建成功";
            }
        }
        return "新建失败";
    }


}
